/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PCR;

import graphics.PrintPreviewer;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import utilities.ImagePrinter;

/**
 *
 * @author icdev
 */
public class JPrintPrivewFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form JPrintPrivewFrame
     */
    private PrintPreviewer panel;
    private JScrollPane jScrollPane1;

    /*
     * This function used to set printpreview panel
     */
    public void setPanel(PrintPreviewer pnl) {
        panel = pnl;
    }

    /*
     * This function used to set the scroll pane to the frame
     */
    public void setJScrollPanel() {
        this.jScrollPane1 = new JScrollPane(panel);
        jScrollPane1.setLocation(new Point(0, 30));
        Dimension dim = new Dimension(this.getSize().width - 11, this.getSize().height - 84);
        jScrollPane1.setSize(dim);

        this.add(jScrollPane1);

        jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setFocusable(false);
        jScrollPane1.setPreferredSize(dim);

    }
    
    /*
     * Class public constructor
     */
    public JPrintPrivewFrame() {
        initComponents();
        jToggleButton3.setSelected(true);
        setMenuesAvailability(jRadioButtonMenuItem4);

    }

    public JScrollPane getPrivewArea() {
        return this.jScrollPane1;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu3 = new javax.swing.JMenu();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jRadioButtonMenuItem3 = new javax.swing.JRadioButtonMenuItem();
        jToolBar1 = new javax.swing.JToolBar();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
        jRadioButtonMenuItem4 = new javax.swing.JRadioButtonMenuItem();

        jMenu3.setText("jMenu3");

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");

        jRadioButtonMenuItem3.setSelected(true);
        jRadioButtonMenuItem3.setText("jRadioButtonMenuItem3");

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setAutoscrolls(true);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jToolBar1.setFloatable(false);

        jToggleButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/gnome_zoom_fit_best.png"))); // NOI18N
        jToggleButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButton3.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jToggleButton3jToggleButton1StateChanged(evt);
            }
        });
        jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton3jToggleButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jToggleButton3);

        jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/gnome_zoom_in.png"))); // NOI18N
        jToggleButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButton1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jToggleButton1StateChanged(evt);
            }
        });
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jToggleButton1);

        jToggleButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/gnome_zoom_out.png"))); // NOI18N
        jToggleButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToggleButton2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jToggleButton1StateChanged(evt);
            }
        });
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jToggleButton2);

        jMenu1.setText("File");

        jMenuItem1.setText("Print");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Close");
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenu4.setText("Zoom");

        jRadioButtonMenuItem1.setSelected(true);
        jRadioButtonMenuItem1.setText("Zoom In");
        jMenu4.add(jRadioButtonMenuItem1);

        jRadioButtonMenuItem2.setSelected(true);
        jRadioButtonMenuItem2.setText("Zoom Out");
        jMenu4.add(jRadioButtonMenuItem2);

        jRadioButtonMenuItem4.setSelected(true);
        jRadioButtonMenuItem4.setText("Scale");
        jMenu4.add(jRadioButtonMenuItem4);

        jMenu2.add(jMenu4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(506, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    static int x;
    static int y;
    String zoomStatus;

    public String maginfingStatus() {

        return zoomStatus;

    }

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        JToggleButton curBtn = (JToggleButton) evt.getSource();
        String cursorPath = "";
        if (curBtn.equals(jToggleButton1)) {
            jToggleButton2.setSelected(false);
            cursorPath = "gnome_zoom_in.png";
        } else if (curBtn.equals(jToggleButton2)) {
            jToggleButton1.setSelected(false);
            cursorPath = "gnome_zoom_out.png";
        }


        panel.setZoomStatus(zoomStatus);

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Cursor c;
        try {
            c = toolkit.createCustomCursor(ImageIO.read(this.getClass().getResourceAsStream("/icons/" + cursorPath)), new Point(0, 0), "MyCursor"); //toolkit.createCustomCursor(image, new Point(0, 0), "img");
            panel.setCursor(c);
        } catch (IOException ex) {
        }


    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jToggleButton1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jToggleButton1StateChanged
        if (jToggleButton1.isSelected() && !jToggleButton2.isSelected()) {
            zoomStatus = "zoomin";
        } else if (!jToggleButton1.isSelected() && jToggleButton2.isSelected()) {
            zoomStatus = "zoomout";
        } else if (!jToggleButton1.isSelected() && !jToggleButton2.isSelected()) {
            zoomStatus = "scaled";
        }
    }//GEN-LAST:event_jToggleButton1StateChanged

    private void jToggleButton3jToggleButton1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jToggleButton3jToggleButton1StateChanged
        zoomStatus = "scaled";
        jToggleButton1.setSelected(false);
        jToggleButton2.setSelected(false);
        jToggleButton3.setSelected(false);
    }//GEN-LAST:event_jToggleButton3jToggleButton1StateChanged

    private void jToggleButton3jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton3jToggleButton1ActionPerformed

        zoomStatus = "scaled";

        // PrintPreviewer prntPrev = (PrintPreviewer) this.jScrollPane1.getComponent(0);
        panel.setSize(500, getPrivewArea().getSize().height);;
        int a = jScrollPane1.getSize().width / 2 - panel.getSize().width / 2;
        int b = jScrollPane1.getSize().height / 2 - panel.getSize().height / 2;

        panel.setLocation(a, b);

        panel.setZoomStatus(zoomStatus);
        panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

        jToggleButton1.setSelected(false);
        jToggleButton2.setSelected(false);
        jToggleButton3.setSelected(false);
        jToggleButton3.grabFocus();
    }//GEN-LAST:event_jToggleButton3jToggleButton1ActionPerformed

    private void setMenuesAvailability(JRadioButtonMenuItem curItm) {
        JRadioButtonMenuItem[] rbitems = {jRadioButtonMenuItem1, jRadioButtonMenuItem2, jRadioButtonMenuItem3, jRadioButtonMenuItem4};
        curItm.setSelected(true);
        for (JRadioButtonMenuItem rbitm : rbitems) {
            if (rbitm.equals(curItm)) {

                continue;
            }
            rbitm.setSelected(false);
        }

    }

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        ImagePrinter.printImage(this.panel);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        Dimension dim = new Dimension(this.getSize().width - 11, this.getSize().height - 84);
        jScrollPane1.setSize(dim);
    }//GEN-LAST:event_formComponentResized
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem3;
    private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem4;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
